package com.teach.demo02;

import java.util.ArrayList;
import java.util.List;

/**
 * 自动售卖机
 * @author J.L.Zhou
 *
 */
public class Machine {
	
	private List<Product> list = new ArrayList<>();
	
	private long idCount=1;
	
	
	/**
	 * 添加一个商品
	 * @param product
	 */
	public void add(Product product){
		product.setId(idCount++);
		list.add(product);
	}
	
	public List<CountResult> list(){
		List<CountResult> ret = new ArrayList<>();
		for(Product p: list){
			CountResult cr = null;
//			for(CountResult c : ret){
//				if(c.getName().equals(p.getName().getTypeName())&&c.getBrand().equals(p.getBrand())){
//					cr = c;
//					break;
//				}
//			}
//			System.out.println(p);
			CountResult search = new CountResult();
			search.setName(p.getName().getTypeName());
			search.setBrand(p.getBrand());
//			System.out.println(ret.indexOf(search));
			int index = ret.indexOf(search);
			if(index>-1){
				cr = ret.get(index);
			}
			
			
			if(cr==null){
				cr = new CountResult();
				cr.setName(p.getName().getTypeName());
				cr.setBrand(p.getBrand());
				cr.setPrice(p.getPrice());
				cr.setNum(1);
				ret.add(cr);
			}else{
				cr.setNum(cr.getNum()+1);
			}
		}
		return ret;
	}


	@Override
	public String toString() {
		return "Machine:"+list.toString();
	}
	
}
